<template>
  <div>
    <x-header :left-options="{showBack: true,preventGoBack:true}"
              @on-click-back="goBack" theme="" class="queryHeader">新增责任
    </x-header>
    <group>
      <selector :value-map="['value','text']" direction="rtl" title="责任选择" v-model="form.dutyCode"
                @on-change="dutyChange"
                :options="duties" placeholder="请选择责任">
      </selector>
    </group>
    <group :title="calcAmnt" v-if="form.dutyCode !== ''">
      <x-input text-align="right" style="padding: 12px 15px;" type="number" :is-type="rule" :debounce="1000"
               @on-change="getStandardPre"
               v-model="form.amnt" title="保险金额" v-if="this.currentDutyLists.indexOf(0)>=0"
               placeholder="请填写保险金额"></x-input>
      <selector :value-map="['value','text']" direction="rtl" title="残疾评定标准"
                v-if="this.currentDutyLists.indexOf(1)>=0" @on-change="getStandardPre"
                v-model="form.disableAsstandard" :options="disableAsstandards" placeholder="请选择评定标准">
      </selector>
      <selector :value-map="['value','text']" direction="rtl" title="行标赔付比例"
                v-if="checkStandardViewOfIndustry" @on-change="getStandardPre"
                v-model="form.industryPaymentProp" :options="industryPaymentProps" placeholder="请选择行标比例">
      </selector>
      <selector :value-map="['value','text']" direction="rtl" title="工标赔付比例"
                v-if="checkStandardViewOfJob" @on-change="getStandardPre"
                v-model="form.jobPaymentProp" :options="jobPaymentProps" placeholder="请选择工标比例">
      </selector>
      <selector :value-map="['value','text']" direction="rtl" title="免赔额(元/次)"
                v-if="this.currentDutyLists.indexOf(2)>=0" @on-change="getStandardPre"
                v-model="form.deductible" :options="deductibles" placeholder="请选择免赔额">
      </selector>
      <selector :value-map="['value','text']" direction="rtl" title="赔付比例"
                v-if="this.currentDutyLists.indexOf(3)>=0" @on-change="getStandardPre"
                v-model="form.deductibleRate" :options="deductibleRates" placeholder="请选择赔付比例">
      </selector>
      <selector :value-map="['value','text']" direction="rtl" title="免赔天数"
                v-if="this.currentDutyLists.indexOf(4)>=0" @on-change="getStandardPre"
                v-model="form.deductibleDats" :options="deductibleDatses" placeholder="请选择免赔天数">
      </selector>
      <x-input text-align="right" style="padding: 12px 15px;" v-if="this.currentDutyLists.indexOf(5)>=0" :readonly="true"
               type="number" :is-type="rule" v-model="form.standardPremium" title="建议保费(元)"
               placeholder="保费自动生成"></x-input>
      <x-input text-align="right" style="padding: 12px 15px;" v-if="this.currentDutyLists.indexOf(6)>=0"
               type="number" :is-type="rule" v-model="form.currentPremium" title="当前保费(元)"
               placeholder="请填写当前保费"></x-input>
    </group>
    <box gap="10px 10px">
      <x-button type="primary" class="positiveBtn" @click.native="addInsurance">添加
      </x-button>
    </box>
    <popup v-model="showToast" position="top" :show-mask="false">
      <div class="position-vertical-demo">
        {{toastTxt}}
      </div>
    </popup>
  </div>
</template>
<script>
  import {XHeader, Group, XInput, Selector, Popup, XButton, Box, ViewBox} from 'Vux'
  import Util from "util"
  export default {
    components: {
      XHeader,
      Group,
      XInput,
      Selector,
      Popup,
      XButton,
      Box,
      ViewBox
    },
    data(){
      return {
        showToast: false,
        toastTxt: "",
        currentDutyLists: [],
        form: {
          dutyName: "",
          dutyCode: "",
          amnt: "",
          disableAsstandard: null,
          industryPaymentProp: 4,
          jobPaymentProp: 1,
          deductible: "100",
          deductibleRate: "0.9",
          deductibleDats: "0",
          originPremium: "",
          currentPremium: "",
          standardPremium: "",
          premiumRate: "",
          discount: ""
        },
        duties: [
          {
            lists: [0, 5, 6],
            value: "601001",
            text: "团体意外身故"
          },
          {
            lists: [0, 1, 5, 6],
            value: "601002",
            text: "团体意外伤残"
          },
          {
            lists: [0, 2, 3, 5, 6],
            value: "602001",
            text: "附加团体意外伤害医疗"
          },
          {
            lists: [0, 4, 5, 6],
            value: "603001",
            text: "团体意外住院补贴"
          }
        ],
        disableAsstandards: [
          {
            value: 1,
            text: "行业标准",
          },
          {
            value: 2,
            text: "工伤标准"
          },
          {
            value: 3,
            text: "工伤按工伤、非工伤按行标"
          }
        ],
        industryPaymentProps: [
          {
            value: 1,
            text: "3%-5%-7%",
          },
          {
            value: 2,
            text: "5%-10%-15%"
          },
          {
            value: 3,
            text: "10%-12%-18%"
          },
          {
            value: 4,
            text: "10%-20%-30%",
          }
        ],
        jobPaymentProps: [
          {
            value: 1,
            text: "3%-5%-7%",
          },
          {
            value: 2,
            text: "5%-10%-15%"
          },
          {
            value: 3,
            text: "10%-12%-18%"
          },
          {
            value: 4,
            text: "10%-20%-30%",
          }
        ],
        deductibles: [
          {
            value: 0,
            text: "0"
          },
          {
            value: 50,
            text: "50"
          },
          {
            value: 100,
            text: "100"
          },
          {
            value: 200,
            text: "200"
          },
          {
            value: 300,
            text: "300"
          },
          {
            value: 400,
            text: "400"
          }
        ],
        deductibleRates: [
          {
            value: 0.7,
            text: "0.7"
          },
          {
            value: 0.75,
            text: "0.75"
          },
          {
            value: 0.8,
            text: "0.8"
          },
          {
            value: 0.85,
            text: "0.85"
          },
          {
            value: 0.9,
            text: "0.9"
          },
          {
            value: 0.95,
            text: "0.95"
          },
          {
            value: 1,
            text: "1"
          }
        ],
        deductibleDatses: [
          {
            value: 0,
            text: "0"
          },
          {
            value: 1,
            text: "1"
          },
          {
            value: 2,
            text: "2"
          },
          {
            value: 3,
            text: "3"
          },
          {
            value: 4,
            text: "4"
          },
          {
            value: 5,
            text: "5"
          }
        ],
        stateForm: {
          form: {},
          planInfo: {}
        }
      }
    },
    computed: {
      //检测标准显示类
      checkStandardViewOfIndustry(){
        let lists = this.currentDutyLists;
        let as = this.form.disableAsstandard;
        if (lists.indexOf(1) > 0) {
          if (as === 1) {
            return true
          } else if (as === 2) {
            return false
          } else if (as === 3) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      },
      checkStandardViewOfJob(){
        let lists = this.currentDutyLists;
        let as = this.form.disableAsstandard;
        if (lists.indexOf(1) > 0) {
          if (as === 1) {
            return false
          } else if (as === 2) {
            return true
          } else if (as === 3) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      },
      calcAmnt(){
        let price = null;
        if(this.form.amnt !== ""){
          price = '金额大写：' + Util.changeMoneyToChinese(this.form.amnt);
        }else{
          price = '金额大写：'
        }
        return price
      }
    },
    //监听popup
    watch: {
      showToast (val) {
        if (val) {
          setTimeout(() => {
            this.showToast = false
          }, 1500)
        }
      }
    },
    methods: {
      //唤起toast提示
      vuxToast(info){
        this.showToast = true;
        this.toastTxt = info;
      },
      //返回到方案页面
      goBack(){
        if (this.$store.state.app.currentInsurances.length > 0) {
          this.$router.push('./viewInsurance');
        } else {
          this.$router.push('./addScheme');
        }
        this.$store.commit('SAVE_CURRENT_MODIFY_DUTY', null);
      },
      //输入规则
      rule(value){
        if (value < 0) {
          return {
            valid: false,
            msg: '请输入大于零的数值哦'
          }
        } else {
          return {
            valid: true,
            msg: 'ok'
          }
        }
      },
      //责任选择
      dutyChange(value){
        for (let i = 0; i < this.duties.length; i++) {
          if (value === this.duties[i].value) {
            this.form.dutyName = this.duties[i].text;
            this.currentDutyLists = this.duties[i].lists
          }
        }
      },
      //添加责任
      getListNames(lists){
        let lns = [];
        let listNames = ['amnt', 'disableAsstandard', 'deductible', 'deductibleRate', 'deductibleDats', 'currentPremium', 'standardPremium'];
        for (let i = 0; i < lists.length; i++) {
          lns.push(listNames[lists[i]])
        }
        return lns
      },
      addInsurance(){
        this.$store.commit('SAVE_CURRENT_MODIFY_DUTY', null);
        if (this.form.dutyCode !== "") {
          let lists = this.currentDutyLists;
          let flag = true;
          let lns = this.getListNames(lists);
          for (let i = 0; i < lns.length; i++) {
            if (this.form[lns[i]] === "") {
              flag = false;
              break;
            }
          }
          if (flag) {
            let hadDuty = this.$store.state.app.currentInsurances;
            let originDuties = [
              {
                lists: [0, 5, 6],
                value: "601001",
                text: "团体意外身故"
              },
              {
                lists: [0, 1, 5, 6],
                value: "601002",
                text: "团体意外伤残"
              },
              {
                lists: [0, 2, 3, 5, 6],
                value: "602001",
                text: "附加团体意外伤害医疗"
              },
              {
                lists: [0, 4, 5, 6],
                value: "603001",
                text: "团体意外住院补贴"
              }
            ];
            let duties = null;
            if (hadDuty.length === 0) {
              duties = originDuties;
            } else {
              duties = this.duties
            }
            if (lists.indexOf(1) < 0) {
              this.form.disableAsstandard = "";
              this.form.jobPaymentProp = "";
              this.form.industryPaymentProp = "";
            }
            if (lists.indexOf(2) < 0) {
              this.form.deductible = "";
            }
            if (lists.indexOf(3) < 0) {
              this.form.deductibleRate = "";
            }
            if (lists.indexOf(4) < 0) {
              this.form.deductibleDats = "";
            }
            this.$store.commit("SAVE_NEW_INSURANCES", this.form);
            for (let k = 0; k < duties.length; k++) {
              if (this.form.dutyCode === duties[k].value) {
                duties.splice(k, 1)
              }
            }
            this.$store.commit("SAVE_CURRENT_DUTIES", duties);
            this.goBack()
          } else {
            this.vuxToast('请填写完整信息')
          }
        } else {
          this.vuxToast('请先选择责任类型')
        }
      },
      //空转Null
      chargeNull(name){
        let form = this.form;
        if (form[name] === "") {
          return null
        } else {
          return form[name]
        }
      },
      //保费计算
      getStandardPre(){
        let data = this.stateForm;
        let form = this.form;
        let lists = this.currentDutyLists;
        if (lists.indexOf(1) < 0) {
          form.disableAsstandard = "";
          form.jobPaymentProp = "";
          form.industryPaymentProp = "";
        }
        if (lists.indexOf(2) < 0) {
          form.deductible = "";
        }
        if (lists.indexOf(3) < 0) {
          form.deductibleRate = "";
        }
        if (lists.indexOf(4) < 0) {
          form.deductibleDats = "";
        }
        this.$vux.loading.show({
          text: '计算中'
        });
        let obj = {
          body: {
            bussSource: data.form.bussSourceCode,
            serviceRate: eval(data.form.serviceRate * 0.01),
            dutyCode: this.form.dutyCode,
            amnt: form.amnt,
            occupationLvl: data.planInfo.occupationType,
            avgAge: data.planInfo.avgAges,
            insureYear: data.planInfo.insuredLength,
            insureYearFlag: data.planInfo.insuredFlag,
            disableAsstandard: this.chargeNull('disableAsstandard'),
            deductibleDats: this.chargeNull('deductibleDats'),
            deductible: this.chargeNull('deductible'),
            claimRate: this.chargeNull('deductibleRate'),
            claimRateA: this.chargeNull('industryPaymentProp'),
            claimRateB: this.chargeNull('jobPaymentProp'),
            specialRate: data.planInfo.specialAppoints
          }
        };
        $.ajax("../ui/servlet/GCASServlet/calprem", {
          method: "post",
          contentType: "text/json;charset=UTF-8",
          data: JSON.stringify(obj),
          success: (info) => {
            this.$vux.loading.hide();
            if (info.code === 400) {
              this.vuxToast(info.data);
              return
            } else {
              let data = JSON.parse(info);
              if (data.head.returnCode === "0") {
                this.form.standardPremium = data.prem.prem;
                this.form.premiumRate = data.prem.premiumRate;
              }
            }
          },
          error: () => {
            this.$vux.loading.hide();
            this.vuxToast('保费计算失败')
          }
        });
      }
    },
    mounted(){
      if (this.$store.state.app.currentInsurances.length > 0) {
        this.duties = this.$store.state.app.currentDuties;
      } else {
        this.duties = this.duties.splice(0, 1)
      }
      let delDuty = this.$store.state.app.currentDelDuty;
      if (delDuty) {
        this.form = delDuty
      }
      this.stateForm.form = this.$store.state.app.formInfo;
      this.stateForm.planInfo = this.$store.state.app.currentScheme;
    }
  }
</script>
